package masking.wechat.sdk.util;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtils {

	public static final int DEFAULT_CONN_TIMEOUT = 10000;
	public static final int DEFAULT_READ_TIMEOUT = 10000;
	public static final String DEFAULT_UA = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36";

	
	public static String get(String url) throws IOException {
		return get(url, DEFAULT_CONN_TIMEOUT, DEFAULT_READ_TIMEOUT);
	}
	
	/**
	 * 发送get请求获取返回结果
	 * @param url
	 * @param connTimeout
	 * @param readTimeout
	 * @return
	 * @throws IOException
	 */
	public static String get(String url, int connTimeout, int readTimeout) throws IOException {
		BufferedReader reader = null;
        StringBuilder result =new StringBuilder();

		try {
			URL urlObj = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
			conn.setConnectTimeout(connTimeout);
			conn.setReadTimeout(readTimeout);
			conn.setRequestProperty("User-Agent", DEFAULT_UA);

			int httpCode = conn.getResponseCode();
			
			InputStream in = null;
			in = httpCode == 200 ? conn.getInputStream() : conn.getErrorStream();
			reader = new BufferedReader(new InputStreamReader(in));

			String line = null;
			while ((line = reader.readLine()) != null) {
				result.append(line);
				result.append("\n");
			}
			conn.disconnect();
			return result.toString();
		} catch (IOException e) {
			throw e;
		} finally {
			closeStreams(reader);
		}
	}
	
    public static String postJson(String url,String jsonStr)throws IOException{
        OutputStreamWriter out = null;
        BufferedReader reader = null;
        StringBuilder result =new StringBuilder();
        try {
            URL urlObj = new URL(url);
            //建立连接
            HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("connection", "keep-alive");
            conn.setConnectTimeout(DEFAULT_CONN_TIMEOUT);
			conn.setReadTimeout(DEFAULT_READ_TIMEOUT);
            conn.setUseCaches(false);//设置不要缓存
            conn.setInstanceFollowRedirects(true);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.connect();
            //POST请求
            out = new OutputStreamWriter(
                    conn.getOutputStream());
            out.write(jsonStr);
            out.flush();
            //读取响应
            reader = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
            	result.append(line);
            	result.append("\n");
            }
            // 断开连接
            conn.disconnect();
            return result.toString();
        } catch (IOException e) {
	        throw e;
        } finally{
			closeStreams(out, reader);
        }

    }
	

	private static void closeStreams(Closeable... streams) {
		for (Closeable s : streams) {
			if (s == null)
				continue;
			try {
				s.close();
			} catch (IOException e) {
			}
		}
	}
}
